﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace xuyiying
{
    public partial class frmClassUD : Form
    {
        int currindex;
        public frmClassUD()
        {
            InitializeComponent();
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            currindex = dataGridView1.CurrentRow.Index;
            textBox1.Text = dataGridView1.Rows[currindex].Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.Rows[currindex].Cells[1].Value.ToString();
            textBox3.Text = dataGridView1.Rows[currindex].Cells[2].Value.ToString();

        }
        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            SqlOperate obj = new SqlOperate();
            string strSql = "select ClassID,ClassName,yuanxi from T_class1";
            SqlDataReader odr = obj.GetDR(strSql);
            while (odr.Read())
            {

                DataGridViewRow row = new DataGridViewRow();
                int index = dataGridView1.Rows.Add(row);
                dataGridView1.Rows[index].Cells[0].Value = odr.GetValue(0).ToString();
                dataGridView1.Rows[index].Cells[1].Value = odr.GetValue(1).ToString();
                dataGridView1.Rows[index].Cells[2].Value = odr.GetValue(2).ToString();

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlOperate obj = new SqlOperate();
            string strSql = "DELETE FROM T_class1 WHERE ClassID=" + textBox1.Text;
            if (obj.RunSql(strSql) > 0)
            {
                MessageBox.Show("删除记录成功！", "信息提示", MessageBoxButtons.OK);
                dataGridView1.Rows.RemoveAt(currindex);
                textBox2.Text = "";
                textBox3.Text = "";
            }
            else
            { MessageBox.Show("删除记录失败！", "信息提示", MessageBoxButtons.OK); }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            SqlOperate obj = new SqlOperate();
            string strSql = "UPDATE T_class1 SET ClassName ='" + textBox2.Text +"',yuanxi='" + textBox3.Text+"'";
            if (obj.RunSql(strSql) > 0)
            {
                MessageBox.Show("修改记录成功！", "信息提示", MessageBoxButtons.OK);
                //dataGridView1.Rows[currindex].Cells[0].Value = textBox1.Text;
                dataGridView1.Rows[currindex].Cells[1].Value = textBox2.Text;
                dataGridView1.Rows[currindex].Cells[2].Value = textBox3.Text;
      

                textBox2.Text = "";
                textBox3.Text = "";
      

            }
            else
            { MessageBox.Show("修改记录失败！", "信息提示", MessageBoxButtons.OK); }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        
    }
}
